<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../res/layui/css/layui.css">

</head>
<body>
<div class="layui-fluid">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>修改密码</legend>
        <div class="layui-field-box">
            <div class="layui-col-md3">
                <form class="layui-form">
                    <div class="demo-reg-container">
                        <div class="layui-form-item">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-password"></i>
                                </div>
                                <input type="password" name="password" value="" lay-verify="required" lay-vertype="tips" lay-reqtext="请输入新密码" placeholder="新密码" autocomplete="off" class="layui-input" id="reg-password" lay-affix="eye">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-password"></i>
                                </div>
                                <input type="password" name="confirmPassword" value="" lay-verify="required|confirmPassword" lay-vertype="tips" lay-reqtext="请输入确认密码" placeholder="确认密码" autocomplete="off" class="layui-input" lay-affix="eye">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="demo-reg">确认修改</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </fieldset>
</div>
<script src="../res/layui/layui.js"></script>
<script>
    layui.use(function () {
        var form = layui.form;
        var layer = layui.layer;
        var $ = layui.jquery;

        // 自定义验证规则
        form.verify({
            // 确认密码
            confirmPassword: function(value, item){
                var passwordValue = $('#reg-password').val();
                if(value !== passwordValue){
                    return '两次密码输入不一致';
                }
            }
        });

        // 提交事件
        form.on('submit(demo-reg)', function(data){
            var field = data.field; // 获取表单字段值
            //从session缓存中获取数据
            let admin = sessionStorage.getItem('token')
            //将字符串用户信息解析为js对象
            admin = JSON.parse(admin)
            //将对象id添加到字段集中（传递到后台） 
            field['id'] = admin.id
            //删除多余的字段（无需传递到后台的确认密码）
            delete field.confirmPassword
           
            // 此处可执行 Ajax 等操作
            $.ajax({
                url:'http://127.0.0.1/admin/update',
                type:'PUT',
                contentType:'application/json',
                data:JSON.stringify(field),
                success:function(resp){
                    if(resp.code === 0){
                        layer.msg('修改成功,请重新登录',function(){
                            //清除缓存数据
                            sessionStorage.clear()
                            //页面跳转到登录页
                            window.parent.location.href='/login.html'
                        })
                        return
                    }
                    //修改失败
                    layer.alert(resp.msg,{icon:2})
                }
            })

            return false; // 阻止默认 form 跳转
        });

    })
</script>
</body>
</html>